package com.note.chh.notebook.UI.stroke;

import android.content.DialogInterface;
import android.graphics.Color;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.blankj.utilcode.util.TimeUtils;
import com.note.chh.notebook.Api.Constant;
import com.note.chh.notebook.Model.Stroke;
import com.note.chh.notebook.R;
import com.note.chh.notebook.UI.base.BaseActivity;
import com.note.chh.notebook.Uitls.DialogUtils;
import com.note.chh.notebook.Uitls.EventReminderUtils;
import com.note.chh.notebook.Uitls.StrokeUtilsDao;
import com.qmuiteam.qmui.widget.QMUITopBar;
import com.qmuiteam.qmui.widget.dialog.QMUIDialog;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import cn.addapp.pickers.common.LineConfig;
import cn.addapp.pickers.picker.DateTimePicker;

//添加日程
public class AddStrokeActivity extends BaseActivity {


    @BindView(R.id.topbar)
    QMUITopBar topbar;
    @BindView(R.id.mEdit_title)
    EditText mEditTitle;
    @BindView(R.id.mText_startTime)
    TextView mTextStartTime;
    @BindView(R.id.mLinear_startTime)
    LinearLayout mLinearStartTime;
    @BindView(R.id.mText_time)
    TextView mTextTime;
    @BindView(R.id.mLinear_time)
    LinearLayout mLinearTime;
    @BindView(R.id.mEdit_remark)
    EditText mEditRemark;

    private String selectStartTime  = "";//yyyy-MM-dd HH:mm
    private int checkedIndex = 1;//xx分钟前 的索引
    @Override
    protected int bindLayout() {
        return R.layout.activity_add_stroke;
    }

    @Override
    public void initTopBar() {
        super.initTopBar();
        topbar.setBackgroundColor(getResources().getColor(R.color.app_color_blue));
        topbar.addLeftBackImageButton().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        topbar.setTitle(getResources().getString(R.string.add_stroke));
        Button rightButton = topbar.addRightTextButton("确定", R.id.top_bar_right_stroke);
        rightButton.setTextColor(Color.WHITE);
        rightButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String title = mEditTitle.getText().toString().trim();
                if (title.isEmpty()) {
                    showToast(AddStrokeActivity.this,"请输入事件标题");
                } else if (TimeUtils.string2Millis(selectStartTime+":00")<=TimeUtils.string2Millis(TimeUtils.getNowString())) {
                    showToast(AddStrokeActivity.this,"事件开始时间要大于当前时间");
                }else{
                    EventReminderUtils.addCalendarEvent(AddStrokeActivity.this,title,mEditRemark.getText().toString(),selectStartTime,Constant.times[checkedIndex]);
                    List<Stroke> strokes = StrokeUtilsDao.queryAll();
                    if (strokes.size()==0) {
                        Stroke stroke = new Stroke(1,title,selectStartTime,checkedIndex,TimeUtils.getNowString(),mEditRemark.getText().toString(),mTextStartTime.getText().toString(),mTextTime.getText().toString());
                        StrokeUtilsDao.insertOrReplaceStroke(stroke);
                    }else{
                        Stroke stroke = new Stroke(strokes.size()+1,title,selectStartTime,checkedIndex,TimeUtils.getNowString(),mEditRemark.getText().toString(),mTextStartTime.getText().toString(),mTextTime.getText().toString());
                        StrokeUtilsDao.insertOrReplaceStroke(stroke);
                    }
                    showSuccess(AddStrokeActivity.this,"事件添加完成");
                    setResult(Constant.ADD_STROKE_RESULTCODE);
                    mhandler.sendEmptyMessageDelayed(1,2000);
                }
            }
        });
    }
    Handler mhandler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what){
                case 1:
                    finish();
                    break;
            }
        }
    };

    @Override
    public void initView() {
        String[] time = TimeUtils.getNowString().split(" ");
        String[] time1 = time[0].split("-");
        String[] time2 = time[1].split(":");
        mTextStartTime.setText(time1[0]+"年"+time1[1]+"月"+time1[2]+"日"+TimeUtils.getChineseWeek(TimeUtils.getNowString())+" "+time2[0]+":"+time2[1]);
        selectStartTime = time1[0]+"-"+time1[1]+"-"+time1[2]+" "+time2[0]+":"+time2[1];
    }

    @Override
    public void initData() {

    }


    @OnClick({R.id.mLinear_startTime, R.id.mLinear_time})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.mLinear_startTime:
                onYearMonthDayTimePicker();
                break;
            case R.id.mLinear_time:
                SelectTime();
                break;
        }
    }

    private void onYearMonthDayTimePicker() {
        String[] time = selectStartTime.split(" ");
        String[] time1 = time[0].split("-");
        String[] time2 = time[1].split(":");
        DateTimePicker picker = new DateTimePicker(this, DateTimePicker.HOUR_24);
        picker.setDateRangeStart(Integer.parseInt(time1[0]), Integer.parseInt(time1[1]), Integer.parseInt(time1[2]));
        picker.setDateRangeEnd(2025, 11, 11);

        picker.setSelectedItem(Integer.parseInt(time1[0]), Integer.parseInt(time1[1]), Integer.parseInt(time1[2]),Integer.parseInt(time2[0]),Integer.parseInt(time2[1]));
        picker.setTimeRangeStart(0, 0);
        picker.setTimeRangeEnd(23, 59);
        picker.setCanLinkage(false);
        picker.setWeightEnable(true);
        picker.setWheelModeEnable(true);
        LineConfig config = new LineConfig();
        config.setColor(Color.BLUE);//线颜色
        config.setAlpha(120);//线透明度
        config.setVisible(false);//线不显示 默认显示
        picker.setLineConfig(config);
        picker.setOnDateTimePickListener(new DateTimePicker.OnYearMonthDayTimePickListener() {
            @Override
            public void onDateTimePicked(String year, String month, String day, String hour, String minute) {
//                showToast(AddStrokeActivity.this,year + "-" + month + "-" + day + " " + hour + ":" + minute);
                mTextStartTime.setText(year+"年"+month+"月"+day+"日"+TimeUtils.getChineseWeek(year + "-" + month + "-" + day+" "+hour+":"+minute+":"+"00")+" "+hour+":"+minute);
                selectStartTime = year + "-" + month + "-" + day + " " + hour + ":" + minute;
            }
        });
        picker.show();
    }

    private void SelectTime(){
        new QMUIDialog.CheckableDialogBuilder(AddStrokeActivity.this)
                .setCheckedIndex(checkedIndex)
                .addItems(Constant.items, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        checkedIndex = which;
                        mTextTime.setText(Constant.items[which]);
//                        Toast.makeText(AddStrokeActivity.this, "你选择了 " + Constant.items[which], Toast.LENGTH_SHORT).show();
                        dialog.dismiss();
                    }
                })
                .show();

    }
}
